package com.osfw.activiti.service.running.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.osfw.activiti.domain.ProcessApproval;
import com.osfw.activiti.domain.ProcessExecResult;
import com.osfw.activiti.entity.history.ActHiTaskinst;
import com.osfw.activiti.entity.running.ActRuExec;
import com.osfw.activiti.entity.running.ActRuTask;
import com.osfw.activiti.enums.ProcExecResult;
import com.osfw.activiti.mapper.running.ActRuTaskMapper;
import com.osfw.activiti.service.history.IActHiTaskinstService;
import com.osfw.activiti.service.running.IActRuTaskService;
import com.osfw.framework.exception.ServiceException;
import com.osfw.framework.utils.bean.BeanMapper;
import com.osfw.system.entity.manage.SysUser;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;
import java.util.stream.Collectors;

/**
 * 流程任务表 服务层实现
 */
@Service
public class ActRuTaskServiceImpl extends ServiceImpl<ActRuTaskMapper, ActRuTask> implements IActRuTaskService {

    @Autowired
    private IActHiTaskinstService hiTaskinstService;

    @Override
    public boolean init(ProcessExecResult execResult) {
        if(execResult.getResult().intValue() == ProcExecResult.PROCESS_EXECUTION.getResult()){
            List<ActRuTask> ruTasks = execResult.getNextNodeExceinsts().stream().map(x -> {
                ActRuTask ruTask = BeanMapper.map(x, ActRuTask.class);
                ruTask.setOwner(null);
                ruTask.setDelegation(null);
                ruTask.setPriority(50);
                ruTask.setDueDate(null);
                ruTask.setCreateTime(new Date());
                return ruTask;
            }).collect(Collectors.toList());
            boolean saveBatch = saveBatch(ruTasks);
            if(saveBatch) {
                List<ActHiTaskinst> hiTaskinstList = BeanMapper.mapAsList(ruTasks, ActHiTaskinst.class);
                hiTaskinstList.stream().forEach(x -> {
                    x.setClaimTime(null);
                    x.setEndTime(null);
                    x.setDuration(null);
                    x.setResult(null);
                    x.setGoBackActKey(null);
                    x.setGoBackActName(null);
                    x.setComment(null);
                    x.setSignature(null);
                    x.setStamp(null);
                    x.setDeleteReason(null);
                });
                saveBatch = hiTaskinstService.saveBatch(hiTaskinstList);
            }
            return saveBatch;
        }
        return true;
    }

    @Override
    public boolean agree(ProcessApproval approval, SysUser sysUser, Date currentTime, ProcessExecResult execResult) {
        return false;
    }

    @Override
    public boolean goback(ProcessApproval approval, SysUser sysUser, Date currentTime, ProcessExecResult execResult) {

        return false;
    }

    @Override
    public boolean disagree(ProcessApproval approval, SysUser sysUser, Date currentTime, ProcessExecResult execResult) {
        // 更新历史任务审批结果信息
        ActHiTaskinst hiTaskinst = hiTaskinstService.getById(approval.getProcTaskId());
        if(hiTaskinst == null) {
            ServiceException.throw500("该任务已有人审批完成，请刷新页面！");
        }else {
            hiTaskinst.setEndTime(currentTime);
            hiTaskinst.setDuration(currentTime.getTime() - hiTaskinst.getStartTime().getTime());
            hiTaskinst.setResult(approval.getResult());
            hiTaskinst.setComment(approval.getComment());
            hiTaskinst.setSignature(approval.getSignature());
            hiTaskinst.setStamp(approval.getStamp());
            hiTaskinstService.updateById(hiTaskinst);
        }
        // 删除运行任务数据（不再流转下一任务）
        removeById(approval.getProcTaskId());
        return true;
    }

}
